package com.msb.internalcommon.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.msb.internalcommon.constant.IdentityConstants;
import com.msb.internalcommon.constant.TokenTypeConstants;
import com.msb.internalcommon.dto.TokenResult;

import javax.print.DocFlavor;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: WuWen
 * @Date: 2023/3/14 - 03 - 14 - 16:35
 * @Description: com.msb.internalcommon.util
 * @version: 1.0
 */

public class JWTUtils {

    // 盐 secret
    private static final String SIGN = "wWEN@3011";
    //
    private static final String JWT_KEY_Phone = "phone";

    private static final String JWT_KEY_IDENTITY = "identity";

    private static final String JWT_TOKEN_TYPE = "type";

    private static final String JWT_TOKEN_TIME = "tokenTime";

    // 生成token
    public static String generatorToken(String passengerPhone, String identity, String tokenType){

        Map<String,String> map = new HashMap<>();
        map.put(JWT_KEY_Phone,passengerPhone);
        map.put(JWT_KEY_IDENTITY,identity);
        map.put(JWT_TOKEN_TYPE, tokenType);
        // 防止token每次都一样
        map.put(JWT_TOKEN_TIME, Calendar.getInstance().getTime().toString());

        // 设置token过期时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,1);
        Date date = calendar.getTime();

        JWTCreator.Builder builder = JWT.create();
        // payload
        map.forEach(
                builder::withClaim);
        // 整合过期时间
//        builder.withExpiresAt(date);

        // 生成token
        String sign = builder.sign(Algorithm.HMAC256(SIGN));


        return sign;
    }


    //解析token
    public static TokenResult parseToken(String token){

        DecodedJWT verify = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
        String phone = verify.getClaim(JWT_KEY_Phone).asString();
        String identity = verify.getClaim(JWT_KEY_IDENTITY).asString();
        TokenResult tokenResult = new TokenResult();
        tokenResult.setPhone(phone);
        tokenResult.setIdentity(identity);

        return tokenResult;
    }

    // 校验token是否违法
    public static TokenResult checkToken(String token){
        TokenResult tokenResult = null;
        try {
            tokenResult = JWTUtils.parseToken(token);
        } catch (Exception e) {

        }
        return tokenResult;
    }


    public static void main(String[] args) {

        String s = generatorToken("18604476415", IdentityConstants.IDENTITY_PASSENGER,TokenTypeConstants.ACCESS_TOKEN);
        System.out.println("生成的token是："+s);
        TokenResult tokenResult = parseToken(s);
        String phone = tokenResult.getPhone();
        String identity = tokenResult.getIdentity();
        System.out.println("解析出来的手机号："+phone);
        System.out.println("解析出来的身份："+identity);
    }

}
